/**
  * Created by Joshua Conero(O'Uymas) on 2016/11/28 0028.
  * QQ: 957376074
  * E-mail: Brximl@163.com
  */
package conero
import java.io._
import java.text.SimpleDateFormat
import java.util.Date
import java.util.regex.Pattern
import java.lang._
import scala.actors.migration.pattern
import scala.util.Try
class Helper {
  // 构造函数
  val __dir__ = System.getProperty("user.dir")
  val version = "0.0.8"
  val author = "Joshua Doeeking Conero"
  // 返回 基于时间的标识符
  def stamp():String = {
    val d = new Date()
    return d.getTime().toString()
  }
  // 系统时间日期
  def sysdate():String = {date("yyyy-MM-dd HH:mm:ss")}
  // 系统时间
  def systime():String={date("hh:mm:ss")}
  // 当前日期
  def curdate():String = {date("yyyy-MM-dd")}
  // 可格式化时间和日期
  def date(fmt:String):String = {
    var ft = new SimpleDateFormat(fmt)
    return ft.format(new Date())
  }
  // 缺陷是指获取当毫秒数为没有其他数-求事件差
  def detMsec(sta:Float = 0.toFloat):Float ={
    val us = str2int(date("S")).toFloat // 毫秒数
    if(sta == 0) return us
    var det = us-sta
      det/1000
  }
  // 系统事件运行求差
  def dateDiff():dateDiff = new dateDiff()
  // 字符串是否为数值
  def is_numeric(value:String):Boolean = {
    val ptr = Pattern.compile("^[-\\+]?[\\d]*$")
    ptr.matcher(value).matches()
  }
  // 文件内容写入
  def put_content(fname:String,ctt:String=""):Boolean = {
    try {
      val pw = new PrintWriter(fname)
      pw.write(ctt)
      pw.close()
      true
    }catch {
      case e:Exception => false
    }
  }
  // 文本读取
  def get_content(fname:String):String = {
    if(is_file(fname) == false) return ""
    val rd = new BufferedReader(new FileReader(fname))
    var content = ""
    var tmpContent = ""
    do{
      tmpContent = rd.readLine()
      if(tmpContent != null) content += tmpContent
    }while(tmpContent != null)
    rd.close()
    content
  }
  // 文件追加
  def file_append(fname:String,ctt:String):Boolean = {
    try {
      /*
      val pw = new PrintWriter(fname,true)
      pw.write(ctt)
      */
      val bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fname,true)))
      bw.write(ctt)
      bw.close()
      return true
    }catch {
      case e:Exception => false
    }
    //return false
  }
  def is_file(filename:String):Boolean = new File(filename).exists()
  def is_dir(pathname:String):Boolean = new File(pathname).isDirectory()
  // 去除xml中非法的字符串
  def xmlEscape(value:String):String = {
    var tmp = value
    tmp = tmp.replaceAll("&lt;","<")
    tmp = tmp.replaceAll("&gt;",">")
    tmp = tmp.replaceAll("&amp;","&")
    tmp = tmp.replaceAll("&apos;","'")
    tmp = tmp.replaceAll("&apos;","\"")
    tmp
  }
  // 删除文件或者目录
  def unlink(filename:String):Boolean ={new File(filename).delete()}
  def getFname(filename:String,nExt:Boolean = false):String ={
    val f = new File(filename)
    var name = f.getName
    if(nExt == true){
      name = name.replaceAll("."+getFileExt(f),"")
    }
    name.trim
  }
  // 文件的"."号，单引号解析成功，双引号无效.
  def getFileExt(filename:String):String ={// 获取文件类型名称
  val name = new File(filename).getName()
    //return name.substring(filename.lastIndexOf('.')+1) //return name.substring(filename.lastIndexOf(".")+1)
    val arr = name.split('.')
    val len = arr.length
    if(len > 0){
      return arr(len-1)
    }
    ""
  }
  // scala 同名函数不同类型-重载
  def getFileExt(f:File):String = {getFileExt(f.getName())}
  // 字符串转int
  def str2int(value:String):Int = {
    if(value == "") return 0
    try {
      val res = Try(value.trim.toInt)
      return res.toOption.last
    }catch {
      case e:Exception =>
        println(e.getMessage)
        return 0
    }
  }
  // 目录生成 - 目录名/ 多级目录生成
  def mkdir(dir:String,onlyOneClass:Boolean = false):Boolean = {
    val f = new File(dir)
    if(f.isDirectory) return true
    if(onlyOneClass == true) f.mkdir() else f.mkdirs()
  }
  // 数据类型
  def typeof[T](v:T) = v
  // base64编解码
  def base64_decode(text:String):String = {(new Crypto).base64_decode(text)}
  def base64_encode(text:String):String = {(new Crypto).base64_encdoe(text)}
  //(new Crypto).decode(helper.get_content(times).trim,about.name)
  // ----数据库sql自动生成 begin
  def inert(table:String,data:Map[String,String]):String = {
    var sql = "insert into "+table+"() values()"
    for((k,v)<-data){
      sql += ""
    }
    sql
  }
}